package com.mrzhou.core.tool.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author qiang.zhou
 *  java 1.8 新日期api工具类
 */
public class LocalDateUtil {

    /** 默认日期格式 */
    public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_FORMAT = "yyyy-MM-dd";
    public final static String TIME_FORMAT = "HH:mm:ss";


    /**
     * 按默认的格式 格式化时间
     * @param localDate
     * @return
     */
    public static String getDateFormat(LocalDate localDate) {
        return getDateFormat(localDate, DATE_FORMAT);
    }

    /**
     * {@link LocalDate}转字符
     * @param localDate
     * @return
     */
    public static String getDateFormat(LocalDate localDate, String pattern) {
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String getDateFormat(Date date) {
        return getDateFormat(date, DATE_TIME_FORMAT);
    }

    public static String getDateFormat(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 按默认时间格式化时间
     * @param localDateTime
     * @return
     */
    public static String getDateFormat(LocalDateTime localDateTime) {
        return getDateFormat(localDateTime, DATE_TIME_FORMAT);
    }

    /**
     * 指定格式格式化时间
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String getDateFormat(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取当前日期
     * @return
     */
    public static LocalDate now() {
        return LocalDate.now();
    }

    public static LocalDateTime nowTime() {
        return LocalDateTime.now();
    }

    public static Date nowDate() {
        return new Date();
    }

    /**
     * 字符转日期
     * @param str
     * @param format
     * @return
     */
    public static LocalDate parseDate(String str, String format) {
        if (StringUtil.isBlank(str)) {
            return null;
        }
        return LocalDate.parse(str, DateTimeFormatter.ofPattern(format));
    }

    public static LocalDate parseDate(String str) {
        return parseDate(str, DATE_FORMAT);
    }

    /**
     * 字符转日期时间
     * @param str
     * @param format
     * @return
     */
    public static LocalDateTime parseTime(String str, String format) {
        if (StringUtil.isBlank(str)) {
            return null;
        }
        return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(format));
    }

    public static LocalDateTime parseTime(String str) {
        return parseTime(str, DATE_TIME_FORMAT);
    }

    /**
     * 计算指定日期的前相差的diff的日期
     * @param currentDate
     * @param diffDay  相差的天数
     * @return
     */
    public static LocalDate beforeDay(LocalDate currentDate, long diffDay) {
        if (diffDay < 0L) {
            return currentDate;
        }
        return currentDate.plusDays(~(diffDay - 1L));
    }

    /**
     * 计算指定日期的后相差的diff的
     * @param currentDate
     * @param diffDay 相差的天数
     * @return
     */
    public static LocalDate afterDay(LocalDate currentDate, long diffDay) {
        if (diffDay < 0L) {
            return currentDate;
        }
        return currentDate.plusDays(diffDay);
    }

    /**
     * 获取指定日期的前diffMonth月的日期
     * @param currentDate
     * @param diffMonth 相差的月
     * @return
     */
    public static LocalDate beforeMonth(LocalDate currentDate, long diffMonth) {
        if (diffMonth < 0L) {
            return currentDate;
        }
        return currentDate.plusMonths(~(diffMonth - 1));
    }

    /**
     * 获取指定日期的后diffMonth月的日期
     * @param currentDate
     * @param diffMonth 相差的月
     * @return
     */
    public static LocalDate afterMonth(LocalDate currentDate, long diffMonth) {
        if (diffMonth < 0L) {
            return currentDate;
        }
        return currentDate.plusMonths(diffMonth);
    }

    /**
     * 计算两个时间的间隔天数
     * @param oneDate 要计算的第一个日期
     * @param twoDate 药计算的第二个日期
     * @return
     */
    public static long diffDay(LocalDate oneDate, LocalDate twoDate) {
        return Math.abs(oneDate.toEpochDay() - twoDate.toEpochDay());
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    public static LocalDate toLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }

    public static void main(String[] args) {
        LocalDate one = LocalDateUtil.parseDate("2020-09-15");
        LocalDate two = LocalDateUtil.now();
        System.out.println(LocalDateUtil.diffDay(one, two));
    }

}
